interface Settings {
    version: string;
    minValue: number;
    maxValue: number;
    scheduleTime: {
        hour: number;
        minute: number;
    };
    triggerMinutes: number;
    scope: 'all' | 'followed';
    items: {
        phone: boolean;
        tablet: boolean;
        headphone: boolean;
        other: boolean;
    };
}

class SettingsManager {
    private static instance: SettingsManager;
    private settings: Settings = {
        version: '抖音',
        minValue: 2000,
        maxValue: 999999,
        scheduleTime: {
            hour: 0,
            minute: 0
        },
        triggerMinutes: 2,
        scope: 'all',
        items: {
            phone: true,
            tablet: true,
            headphone: true,
            other: true
        }
    };

    private constructor() {}

    static getInstance(): SettingsManager {
        if (!SettingsManager.instance) {
            SettingsManager.instance = new SettingsManager();
        }
        return SettingsManager.instance;
    }

    loadFromUI(): void {
        try {
            // 版本选择
            const versionIndex = ui.versionSpinner.getSelectedItemPosition();
            this.settings.version = versionIndex === 0 ? '抖音' : '抖音极速版';

            // 价值范围
            const minValue = parseInt(ui.minValueInput.text());
            const maxValue = parseInt(ui.maxValueInput.text());
            if (!isNaN(minValue)) this.settings.minValue = minValue;
            if (!isNaN(maxValue)) this.settings.maxValue = maxValue;

            // 时间设置
            this.settings.scheduleTime.hour = ui.timePicker.getHour();
            this.settings.scheduleTime.minute = ui.timePicker.getMinute();

            // 触发间隔
            const triggerMinutes = parseInt(ui.triggerMinutes.text());
            if (!isNaN(triggerMinutes)) this.settings.triggerMinutes = triggerMinutes;

            // 范围设置
            this.settings.scope = ui.allRadio.isChecked() ? 'all' : 'followed';

            // 物品类型设置
            this.settings.items.phone = ui.phoneCheck.isChecked();
            this.settings.items.tablet = ui.tabletCheck.isChecked();
            this.settings.items.headphone = ui.headphoneCheck.isChecked();
            this.settings.items.other = ui.otherCheck.isChecked();
        } catch (e: unknown) {
            if (e instanceof Error) {
                console.error('加载UI设置失败:', e.message);
            }
        }
    }

    applyToUI(): void {
        try {
            // 版本选择
            ui.versionSpinner.setSelection(this.settings.version === '抖音' ? 0 : 1);

            // 价值范围
            ui.minValueInput.setText(this.settings.minValue.toString());
            ui.maxValueInput.setText(this.settings.maxValue.toString());

            // 时间设置
            ui.timePicker.setIs24HourView(true);
            ui.timePicker.setHour(this.settings.scheduleTime.hour);
            ui.timePicker.setMinute(this.settings.scheduleTime.minute);

            // 触发间隔
            ui.triggerMinutes.setText(this.settings.triggerMinutes.toString());

            // 范围设置
            ui.allRadio.setChecked(this.settings.scope === 'all');
            ui.followedRadio.setChecked(this.settings.scope === 'followed');

            // 物品类型设置
            ui.phoneCheck.setChecked(this.settings.items.phone);
            ui.tabletCheck.setChecked(this.settings.items.tablet);
            ui.headphoneCheck.setChecked(this.settings.items.headphone);
            ui.otherCheck.setChecked(this.settings.items.other);
        } catch (e: unknown) {
            if (e instanceof Error) {
                console.error('应用UI设置失败:', e.message);
            }
        }
    }

    saveToStorage(): void {
        try {
            const storage = storages.create("douyin_fudai_settings");
            storage.put('settings', this.settings);
        } catch (e: unknown) {
            if (e instanceof Error) {
                console.error('保存设置失败:', e.message);
            }
        }
    }

    loadFromStorage(): void {
        try {
            const storage = storages.create("douyin_fudai_settings");
            const savedSettings = storage.get('settings');
            if (savedSettings) {
                this.settings = savedSettings;
            }
        } catch (e: unknown) {
            if (e instanceof Error) {
                console.error('加载设置失败:', e.message);
            }
        }
    }

    getSettings(): Settings {
        return { ...this.settings };
    }

    updateSettings(settings: Partial<Settings>): void {
        this.settings = { ...this.settings, ...settings };
    }
}

export default SettingsManager.getInstance(); 